import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:sptapp/config/const.dart';
import 'package:sptapp/moudle/common_info.dart';

import '../../api/interface/home_api.dart';
import '../../api/request/response_data.dart';
import '../../moudle/message.dart';
import '../../moudle/refresh/my_refresh.dart';
import '../../widget/common_widget.dart';

class MonitorListPage extends StatefulWidget {
  final String status;
  const MonitorListPage({super.key, required this.status});

  @override
  State<MonitorListPage> createState() => _MonitorListPageState();
}

class _MonitorListPageState extends State<MonitorListPage> {

  List _list = [];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _getList();
  }

  _getList()async{
    // showLoading();
    // ResponseData res = await HomeAPI.selectEquipmentInfo(widget.status??'');
    // hideLoading();
    getDevicesList((){
      for(var item in baseDevices){
        item['workvalue'] = '-';
        item['acvalue'] = '-';
        item['wdvalue'] = '-';
        if(item['devDictionaries']!=null){
          List arr = item['devDictionaries'] ?? [];
          for(var info in arr){
            if(info['code'] == 'MD'){
              item['workvalue'] = '${info['value']??''}${info['unit']??''}';
            }else if(info['code'] == 'GFO'){
              item['acvalue'] = '${info['value']??''}${info['unit']??''}';
            }else if(info['code'] == 'TI'){
              item['wdvalue'] = '${info['value']??''}${info['unit']??''}';
            }
          }
        }
      }
      if(mounted){
        setState(() {});
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: defaultBgColor,
      body: Container(
        child: EasyRefresh(
          header: myRefreshHeader(),
          footer: myRefreshFooter(),
          onRefresh: ()async{
            _getList();
          },
          onLoad: ()async{

          },
          child: Container(
            child: ListView.builder(
              itemBuilder: (context,index){
                return GestureDetector(
                  onTap: (){
                    Navigator.of(context).pushNamed('InverterDetailPage',arguments: _list[index]['sn']);
                  },
                  child: Container(
                    padding: EdgeInsets.symmetric(horizontal: 30.w,vertical: 30.w),
                    decoration: BoxDecoration(
                      color: Colors.white,
                      border: Border.all(color: Colors.grey,width: 1),
                      // borderRadius: BorderRadius.circular(20.w)
                    ),
                    margin: EdgeInsets.only(bottom: 30.w),
                    child: Row(
                      children: [
                        Container(
                          height: 200.w,
                          width: 100.w,
                          color: Color(0xffeeeeee),
                          child: Text('SPT'),
                          alignment: Alignment.center,
                        ),
                        SizedBox(width: 30.w,),
                        Expanded(child: Container(
                          child: Container(
                            height: 200.w,
                            child: Column(
                              mainAxisAlignment: MainAxisAlignment.spaceAround,
                              children: [
                                Container(
                                  child: Row(
                                    children: [
                                      Container(
                                        child: Text('${_list[index]['sn']??''}'),
                                      ),
                                      SizedBox(width: 10.w,),
                                      Container(
                                        height: 30.w,
                                        width: 30.w,
                                        decoration: BoxDecoration(
                                            color: _list[index]['status'].toString() == '1'? Colors.green:_list[index]['status'].toString() == '0'?Colors.grey:Colors.transparent,
                                            borderRadius: BorderRadius.circular(40.w)
                                        ),
                                      ),
                                      SizedBox(width: 10.w,),
                                      Container(
                                        child: Text( _list[index]['status'].toString() == '1'?monitor_page_item_status_online.tr:_list[index]['status'].toString() == '0'?monitor_page_item_status_offline.tr:''),
                                      )
                                    ],
                                  ),
                                ),
                                Container(
                                  child: Row(
                                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                    children: [
                                      Container(
                                        child: Container(
                                          child: Column(
                                            children: [
                                              Text(_list[index]['workvalue']),
                                              Text(monitor_page_item_work_mode.tr)
                                            ],
                                          ),
                                        ),
                                      ),
                                      Container(
                                        child: Container(
                                          child: Column(
                                            children: [
                                              Text(_list[index]['wdvalue']),
                                              Text(monitor_page_item_wendu.tr)
                                            ],
                                          ),
                                        ),
                                      ),
                                      Container(
                                        child: Container(
                                          child: Column(
                                            children: [
                                              Text(_list[index]['acvalue']),
                                              Text(monitor_page_item_acpl.tr)
                                            ],
                                          ),
                                        ),
                                      ),
                                    ],
                                  ),
                                )
                              ],
                            ),
                          ),
                        ))
                      ],
                    ),
                  ),
                );
              },
              itemCount: _list.length,
              padding: EdgeInsets.zero,
            ),
          ),
        ),
      ),
    );
  }
}
